//
//  detailView.m
//  mark
//
//  Created by student5 on 2021/12/16.
//

#import <Foundation/Foundation.h>
#import "detailView.h"
#import "RecordSetController.h"

@interface detailView()
@property UIButton *backButton;
@property UIButton *setButton;
@property(nonatomic,strong)UINavigationBar *navigationBar;
@property(strong,nonatomic)UIBarButtonItem *set_item;//左边的设置按钮，但是加了好像也没有必要
//@property(strong,nonatomic)UITextView *content;
@property(strong,nonatomic) UIWebView *webView;
@property(nonatomic)float realsize;
@end

@implementation detailView

-(void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];//这个是必须要的，否则动画效果会不太一样
    [self setNavigationbar];//设置导航栏
    [self backbtn];
    [self setbtn];
    [self set_content]; //设置页面的内容
    self.webView.delegate = self;
}




-(void)set_content{
    
    // Determile cache file path
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height - 60;
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, width, height)];
    if([[NSFileManager defaultManager] fileExistsAtPath:self.data_path[3]]){
        NSLog(@"file html is exist");
    }
    NSString *retStr = [NSString stringWithContentsOfFile:self.data_path[3] encoding:NSUTF8StringEncoding error:nil];
    //retStr = @"<p style=text-align:left >　　大约十秒钟之后。</p>";
    NSLog(@"str %@",retStr);
    // Load file in UIWebView
    [webView loadHTMLString:retStr baseURL:nil];  //显示在webview里
    //NSData *PdfData = [self converToPDF];
    //NSLog(@"data ready");
    //NSLog(@"data %@",PdfData);
    //NSData to pdf
    //NSData *contentData               = PdfData;
    //CFDataRef PDFData                 = (__bridge CFDataRef )contentData;
    //CGDataProviderRef provider        = CGDataProviderCreateWithCFData(PDFData);
    //CGPDFDocumentRef genPdf           = CGPDFDocumentCreateWithProvider(provider);
    
    //[webView loadData:PdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; //加载pdfdata
    [webView loadHTMLString:retStr baseURL:nil];  //显示在webview里
    [self.view addSubview:webView];
    self.webView = webView;
    //self.realsize = 100; //每次进detail的时候字体大小都是100
    NSString *size_str = [NSString stringWithContentsOfFile:self.data_path[4] encoding:NSUTF8StringEncoding error:nil];
    self.realsize = [size_str floatValue];
    //[self webViewDidFinishLoad:self.webView];
    //float size = (self.realsize-80)/(200-80);  //调整页面字体大小
    //[self set_content_font_size:size];
    //self.realsize = 150;
    //NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",self.realsize];
    //[_webView stringByEvaluatingJavaScriptFromString:str];
}




- (void)webViewDidFinishLoad:(UIWebView *)webView{  //代理方法
    NSLog(@"adjust!");
    NSString *fontString = [NSString stringWithFormat:@"%f",self.realsize];
    [fontString writeToFile:_data_path[4] atomically:YES encoding:NSUTF8StringEncoding error:nil];//存储字体大小
    NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",self.realsize];
    //NSLog(str);
    [_webView stringByEvaluatingJavaScriptFromString:str];
}




/*- (NSData *)converToPDF{
    NSLog(@"conver");
    UIViewPrintFormatter *fmt = [self.webView viewPrintFormatter];
    UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
    [render addPrintFormatter:fmt startingAtPageAtIndex:0];
    CGRect page;
    page.origin.x=0;
    page.origin.y=0;
    page.size.width=600;
    page.size.height=768;
    CGRect printable=CGRectInset( page, 50, 50 );
    
    [render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
    [render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];
    NSMutableData * pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );
    for (NSInteger i=0; i < [render numberOfPages]; i++) {
        UIGraphicsBeginPDFPage();
        CGRect bounds = UIGraphicsGetPDFContextBounds();
        [render drawPageAtIndex:i inRect:bounds];
    }

    UIGraphicsEndPDFContext();
    return pdfData;
}*/




-(void)set_content_font_size:(float)size{ //设置字体大小
    self.realsize = size*(200-80)+80;
    NSString *fontString = [NSString stringWithFormat:@"%f",self.realsize];
    [fontString writeToFile:_data_path[4] atomically:YES encoding:NSUTF8StringEncoding error:nil];//存储字体大小
    NSString *str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%f%%'",self.realsize];
    //NSLog(str);
    [_webView stringByEvaluatingJavaScriptFromString:str];
}





- (void)backbtn{  //返回主界面
    self.backButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 28, 50, 30)];
    [self.backButton setTitle:@"返回"forState:UIControlStateNormal];
    self.backButton.layer.cornerRadius = 5;
    self.backButton.layer.borderWidth = 1;
    self.backButton.layer.borderColor = [UIColor grayColor].CGColor;
    [self.backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];//添加事件
    
    [self.view addSubview:self.backButton];

}





-(void)backButtonPressed:(id)sender{ //返回按钮点击
    NSLog(@"backButtonPressed");

    [self.father_view sort_time];//在刷新之前给记录按时间排序
    [self.father_view reloadTableViewData];//刷新主页面中的tableview
    //跳转到起始页面
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}




//导航栏
-(void)setNavigationbar{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, screenRect.size.width, 80.0f)];

    //self.navigationItem.rightBarButtonItem = self.set_item;
    //创建UINavigationItem
    UINavigationItem *navigationBarTitle = [[UINavigationItem alloc] initWithTitle:@"记录"];
    NSDictionary *attrs = @{NSFontAttributeName : [UIFont systemFontOfSize:20]};
    [navigationBar setTitleTextAttributes:attrs];
    [navigationBar pushNavigationItem:navigationBarTitle animated:YES];
    [self.view addSubview: navigationBar];
    [navigationBar setItems:[NSArray arrayWithObject: navigationBarTitle]];//这句不知道用来作什么，去掉了也一样
}




- (void)setbtn{ //详细设置
    self.setButton = [[UIButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-70, 28, 50, 30)];
    [self.setButton setTitle:@"设置"forState:UIControlStateNormal];
    self.setButton.layer.cornerRadius = 5;
    self.setButton.layer.borderWidth = 1;
    self.setButton.layer.borderColor = [UIColor grayColor].CGColor;
    [self.setButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.setButton addTarget:self action:@selector(setButtonPressed:) forControlEvents:UIControlEventTouchUpInside];//添加事件
    
    [self.view addSubview:self.setButton];
}




-(void)setButtonPressed:(id)sender{//添加新的记录的函数
    NSLog(@"setButtonPressed");
    RecordSetController* re = [[RecordSetController alloc] init];
    re.modalPresentationStyle = 0;
    re.father_view = self;
    re.data_path = self.data_path; //把文件路径传到子页面
    re.slider_value = (self.realsize-80)/(200-80);
    [self presentViewController:re animated:YES completion:nil];
}




- (void)send_delete_message{
    [self.father_view sort_time];//在刷新之前给记录按时间排序
    [self.father_view deleteData:self.data_path];
}

@end
